<template>
    <el-card class="box-card" style="margin: 10px 0px;">
        <!-- 标头 -->
        <div slot="header" class="clearfix">
            <el-tabs class="tab" v-model="activeName">
                <el-tab-pane label="销售额" name="sale"></el-tab-pane>
                <el-tab-pane label="访问量" name="visite"></el-tab-pane>
            </el-tabs>
            <div class="right">
                <span @click="setTaday">今日</span>
                <span @click="setCurrentWeek">本周</span>
                <span @click="setCurrentMonth">本月</span>
                <span @click="setCurrentYear">本年</span>
                <el-date-picker v-model="myData" class="date" type="daterange" range-separator="-"
                    start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd"></el-date-picker>
            </div>
        </div>
        <!-- 内容 -->
        <div>
            <el-row :gutter="10">
                <el-col :span="18">
                    <!-- 容器 -->
                    <div class="charts" ref="charts"></div>
                </el-col>
                <el-col :span="6" class="right">
                    <h3> 门店{{ title }}排名 </h3>
                    <ul>
                        <li>
                            <span class="rindex">0</span>
                            <span>肯德基</span>
                            <span class="rvalue">123456</span>
                        </li>
                        <li>
                            <span class="rindex">1</span>
                            <span>肯德基</span>
                            <span class="rvalue">123456</span>
                        </li>
                        <li>
                            <span class="rindex">3</span>
                            <span>肯德基</span>
                            <span class="rvalue">123456</span>
                        </li>
                    </ul>
                </el-col>
            </el-row>
        </div>
    </el-card>
</template>

<script>
import * as echarts from 'echarts';
import dayjs from "dayjs";
import { mapState } from 'vuex';
import { title } from '@/settings';
export default {
    name: 'Sale',
    data() {
        return {
            activeName: 'sale',
            myData: [],
            mycharts: null,
        };
    },
    mounted() {
        this.initCharts();
    },
    methods: {
        initCharts() {
            this.mycharts = echarts.init(this.$refs.charts);
            this.mycharts.setOption({
                title: {
                    text: this.title + "趋势",
                },
                tooltip: {
                    trigger: "axis",
                    axisPointer: {
                        type: "shadow",
                    },
                },
                grid: {
                    left: "3%",
                    right: "4%",
                    bottom: "3%",
                    containLabel: true,
                },
                xAxis: [
                    {
                        type: "category",
                        data: [],
                        axisTick: {
                            alignWithLabel: true,
                        },
                    },
                ],
                yAxis: [
                    {
                        type: "value",
                    },
                ],
                series: [
                    {
                        name: "Direct",
                        type: "bar",
                        barWidth: "60%",
                        data: [],
                        color: "yellowgreen",
                    },
                ],
            });
        },
        //本天
        setTaday() {
            const day = dayjs().format("YYYY-MM-DD");
            this.myData = [day, day];
        },
        //本周
        setCurrentWeek() {
            const start = dayjs().day(1).format("YYYY-MM-DD");
            const end = dayjs().day(7).format("YYYY-MM-DD");
            this.myData = [start, end];
        },
        //本月
        setCurrentMonth() {
            const start = dayjs().startOf("month").format("YYYY-MM-DD");
            const end = dayjs().endOf("month").format("YYYY-MM-DD");
            this.myData = [start, end];
        },
        //本年
        setCurrentYear() {
            const start = dayjs().startOf("year").format("YYYY-MM-DD");
            const end = dayjs().endOf("year").format("YYYY-MM-DD");
            this.myData = [start, end];
        },

    },
    computed: {
        title() {
            return this.activeName == "sale" ? "销售额" : "访问量";
        },
        ...mapState({ listData: state => state.home.list })
    },
    watch: {
        title: {
            handler(newVal, oldVal) {
                this.mycharts.setOption({
                    title: {
                        text: this.title
                    },
                    xAxis: {
                        data: this.title === '销售额' ? this.listData.orderFullYearAxis : this.listData.userFullYearAxis,
                    },
                    series: {
                        name: "Direct",
                        type: "bar",
                        barWidth: "60%",
                        data: this.title === '销售额' ? this.listData.orderFullYear : this.listData.userFullYear,
                        color: "yellowgreen",
                    }
                })
            },
            immediate: true  //首次执行  
        }


    }


}
</script>

<style scoped>
.clearfix {
    position: relative;
    display: flex;
    justify-content: space-between;
}

.tab {
    width: 100%;
}

.right {
    position: absolute;
    right: 0px;
}

.date {
    width: 250px;
    margin: 0px 20px;
}

.right span {
    margin: 0px 10px;
}

.charts {
    width: 100%;
    height: 300px;
}

ul {
    list-style: none;
    width: 100%;
    height: 300px;
    padding: 0px;
}

ul li {
    height: 8%;
    margin: 10px 0px;
}

.rindex {
    float: left;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: black;
    color: white;
    text-align: center;
}

.rvalue {
    float: right;
}
</style>